namespace cnVery.Cms.UI
{
    using cnVery.Cms.Kernel;
    using cnVery.Utils;
    using System;
    using System.IO;
    using System.Web;

    public class Images : UI_Main
    {
        private void DownloadFile(string filename)
        {
            string path = HttpContext.Current.Request.MapPath(filename);
            if (File.Exists(path))
            {
                base.Response.ClearContent();
                base.Response.ContentType = "image/*";
                base.Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filename));
                base.Response.WriteFile(path);
                base.Response.End();
            }
            else
            {
                base.Response.ClearContent();
                base.Response.ContentType = "image/*";
                base.Response.AddHeader("Content-Disposition", "attachment; filename=no.gif");
                base.Response.WriteFile(HttpContext.Current.Request.MapPath("images/no.gif"));
                base.Response.End();
            }
        }

        private void InitializeComponent()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            string fn = this.Get("path");
            if (Fetch.IsGetFromAnotherDomain || Fetch.IsPostFromAnotherDomain)
            {
                fn = @"images\no.gif";
            }
            fn = Tools.DecodeFileName(fn);
            this.DownloadFile(fn);
        }
    }
}

